<?php
/**
 * User class
 * 
 * @author Mathieu Pheulpin - sokaru91@gmail.com
 *
 */
class Mac_User
{
    const SESSION_NAMESPACE = 'mac_user';
    
    const FIELD_MAC_ID = 'mac_id';
    const FIELD_GROUPS = 'groups';
    const FIELD_STATUS = 'status';
    
    const STATUS_ANONYMOUS = 'anonymous';
    const STATUS_LOGGED_IN = 'loggedin';
    
    private $session;
    
    private static $instance = null;
    
    /**
     * 
     * @return Mac_User
     */
    public static function getInstance()
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    private function __construct ()
    {
        $this->session = Mac_Session::getNamespace(self::SESSION_NAMESPACE);
        if ($this->session->get(self::FIELD_STATUS) === null) {
            $this->session->set(self::FIELD_GROUPS, array());
            $this->session->set(self::FIELD_STATUS, self::STATUS_ANONYMOUS);
        }
    }
    
    public static function login ($login, $passwd)
    {
        $session = self::getInstance()->session;
        if ($session->get(self::FIELD_STATUS) !== self::STATUS_ANONYMOUS) {
            throw new Mac_User_Exception_AlreadyLoggedIn('A current user is already logged in this session, unable to login new user');
        }
        $table_config = Mac_Configuration::get('user', 'db');
        $user_result = Mac_Db_Query_Select::fields(array(
                $table_config['field_id'],
                $table_config['field_groups']))
            ->from(Mac_Db_Clause_From::tables($table_config['table_name']))
            ->where(
                Mac_Db_Clause_Where::clauses($table_config['field_login'], Mac_Db_Type_Op::EQUALS, $login)
                ->andWhere($table_config['field_password'], Mac_Db_Type_Op::EQUALS, $passwd))
            ->limit(1)
            ->execute();
        
        if ($user_result !== false) {
            if ($data = mysql_fetch_assoc($user_result)) {
                $session->set(self::FIELD_MAC_ID, $data[$table_config['field_id']]);
                $session->set(self::FIELD_GROUPS, explode(';', $data[$table_config['field_groups']]));
                $session->set(self::FIELD_STATUS, self::STATUS_LOGGED_IN);
                return true;
            }
        }
        return false;
    }
    
    public static function logout ()
    {
        $session = self::getInstance()->session;
        if ($session->get(self::FIELD_STATUS) === self::STATUS_ANONYMOUS) {
            throw new Mac_User_Exception_NotLoggedIn('No current user logged in found, unable to logout user');
        }
        $session->set(self::FIELD_MAC_ID, null);
        $session->set(self::FIELD_GROUPS, array());
        $session->set(self::FIELD_STATUS, self::STATUS_ANONYMOUS);
    }
    
    public static function isAuthentified ()
    {
        return (self::getInstance()->session->get(self::FIELD_STATUS) === self::STATUS_LOGGED_IN);
    }
    
    public static function isAuthorized ($granted_groups)
    {
        if (($granted_groups === null)
        || (sizeof($granted_groups) === 0)
        || (sizeof(array_intersect($granted_groups, self::getInstance()->session->get(self::FIELD_GROUPS))) > 0)) {
            return true;
        } else {
            return false;
        }
    }
    
    public function getId ()
    {
        return $this->session->get(self::FIELD_MAC_ID);
    }
}